package zjc.DeviceManage.service.Imp;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import zjc.DeviceManage.fragment.HomeFragment;
import zjc.DeviceManage.model.DeviceList;
import zjc.DeviceManage.util.MyAppliacation;
import zjc.DeviceManage.util.MyHttpUtil;

/**
 * 作者：zxl on 2020/7/16 22:55
 * 邮箱：510252916@qq.com
 */
public class DeviceServiceImp {
    private DeviceList deviceListFromJson;
    private void parseJSONtoDeviceList(String responseData)  {

        Gson gson=new Gson();

        deviceListFromJson = gson.fromJson(responseData,

                new TypeToken<DeviceList>(){}.getType());

        Log.i("zjc",deviceListFromJson.toString());

    }
    // 添加回调类

    private HomeFragment homeFragment;

// 注入回调类的构造函数

    public DeviceServiceImp(HomeFragment homeFragment) {

        this.homeFragment = homeFragment;

    }


    public void findAllDevice() {

        // 构造findAllDevice的tomcat服务请求URL

        String findAllDeviceURL = MyHttpUtil.host + "/DeviceManage/findAllDevice";

        MyHttpUtil.sendOkhttpGetRequest(findAllDeviceURL, new Callback() {

            public void onFailure(Call call, IOException e) {

                Log.i("zjc","web接口服务连接失败，请确保主机ip地址是否正确， 然后打开tomcat服务器");

                       MyAppliacation.subThreadToast("web接口服务连接失败，请确保主机 ip地址是否正确，然后打开tomcat服务器");

            }

            public void onResponse(Call call, Response response) throws IOException {

                parseJSONtoDeviceList(response.body().string());

                homeFragment.showAllDeviceCallback(deviceListFromJson);

            }

        });

    }
    public void findDeviceByDeviceClassId(int deviceClassId){

        // 构造findDeviceByDeviceClassId的tomcat服务请求URL

        String findDeviceByDeviceClassId = MyHttpUtil.host + "/DeviceManage/findDeviceByDeviceClassId?deviceClassId=" + new Integer(deviceClassId).toString();

        MyHttpUtil.sendOkhttpGetRequest(findDeviceByDeviceClassId, new Callback() {

            public void onFailure(Call call, IOException e) {

                Log.i("zjc","web接口服务连接失败，请确保主机ip地址是否正确， 然后打开tomcat服务器");

                       MyAppliacation.subThreadToast("web接口服务连接失败，请确保主机ip 地址是否正确，然后打开tomcat服务器");

            }

            public void onResponse(Call call, Response response) throws IOException {

                parseJSONtoDeviceList(response.body().string());

                homeFragment.showDeviceByDeviceClassIdCallback(deviceListFromJson);

            }

        });

    }
            public void onResponse(Call call, Response response) throws IOException {

                parseJSONtoDeviceList(response.body().string());

                homeFragment.showDeviceByDeviceClassIdCallback(deviceListFromJson);

            }

        }


